package com.itboy.mall.sms.skufullreduction.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.itboy.common.utils.PageUtils;
import com.itboy.common.core.page.PagePlus;
import com.itboy.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.itboy.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.itboy.mall.sms.skufullreduction.bo.MallSkuFullReductionAddBo;
import com.itboy.mall.sms.skufullreduction.bo.MallSkuFullReductionQueryBo;
import com.itboy.mall.sms.skufullreduction.bo.MallSkuFullReductionEditBo;
import com.itboy.mall.sms.skufullreduction.domain.MallSkuFullReduction;
import com.itboy.mall.sms.skufullreduction.mapper.MallSkuFullReductionMapper;
import com.itboy.mall.sms.skufullreduction.vo.MallSkuFullReductionVo;
import com.itboy.mall.sms.skufullreduction.service.IMallSkuFullReductionService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 商品满减信息Service业务层处理
 *
 * @author itboy
 * @date 2021-08-08
 */
@Service
public class MallSkuFullReductionServiceImpl extends ServicePlusImpl<MallSkuFullReductionMapper, MallSkuFullReduction> implements IMallSkuFullReductionService {

    @Override
    public MallSkuFullReductionVo queryById(Long id){
        return getVoById(id, MallSkuFullReductionVo.class);
    }

    @Override
    public TableDataInfo<MallSkuFullReductionVo> queryPageList(MallSkuFullReductionQueryBo bo) {
        PagePlus<MallSkuFullReduction, MallSkuFullReductionVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), MallSkuFullReductionVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<MallSkuFullReductionVo> queryList(MallSkuFullReductionQueryBo bo) {
        return listVo(buildQueryWrapper(bo), MallSkuFullReductionVo.class);
    }

    private LambdaQueryWrapper<MallSkuFullReduction> buildQueryWrapper(MallSkuFullReductionQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<MallSkuFullReduction> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSkuId() != null, MallSkuFullReduction::getSkuId, bo.getSkuId());
        lqw.eq(bo.getFullPrice() != null, MallSkuFullReduction::getFullPrice, bo.getFullPrice());
        lqw.eq(bo.getReducePrice() != null, MallSkuFullReduction::getReducePrice, bo.getReducePrice());
        lqw.eq(bo.getAddOther() != null, MallSkuFullReduction::getAddOther, bo.getAddOther());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(MallSkuFullReductionAddBo bo) {
        MallSkuFullReduction add = BeanUtil.toBean(bo, MallSkuFullReduction.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(MallSkuFullReductionEditBo bo) {
        MallSkuFullReduction update = BeanUtil.toBean(bo, MallSkuFullReduction.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(MallSkuFullReduction entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
